package com.ymatou.iapi.productmanager.testcase;
import java.util.*;
import java.util.stream.Collectors;

import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.business.service.ProductPriceCallService;
import com.ymttest.utils.YMTDateUtil;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productmanager.parameter.productExistsActivity.CheckProductInActivityResp;
import com.ymatou.iapi.productmanager.parameter.productExistsActivity.ProductInActivityInfoBean;
import com.ymatou.iapi.productmanager.service.CheckProductExistsActivityCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ProductManagerCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.business.service.productdata.bean.ProductBean;
import com.ymttest.business.service.productdata.impl.ProductFactory;
import com.ymttest.common.order.userquery.util.HttpClientUtil;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.annotation.product.IntergratedProductMapper;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.compare.AssertHelper;

public class Ts_CheckProductExistsActivity {
	private static IntergratedProductMapper intergratedProductMapper = SqlMapperFactory.getIntergratedProductMapper();
	private static Integer sellerId = Integer.valueOf(EnvSetup.getData("productpriceSellerId"));
	private static String sellerName = EnvSetup.getData("productpriceSellerName");
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取商品参加活动信息");
		intergratedProductMapper.updateProductoffSale(sellerId);
	}
	
	@Before
	public void caseUp() {
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new CheckProductExistsActivityCall().getOpurl());
	}
	
	@After
	public void caseDown() {
		Logger.end(); 
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CheckProductExistsActivity_001() throws Exception {
		Logger.start(true,"获取商品参加活动信息 普通商品");
		try{
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProductOnSale(addProdReq).get("Data").getAsJsonObject().get("ProductId").getAsString();
			
			JSONObject ret = new CheckProductExistsActivityCall().call(sellerId, productId);
			
			checkProductExistsActivity(productId,Arrays.asList(),ret);
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CheckProductExistsActivity_002() throws Exception {
		Logger.start(true,"获取商品参加活动信息 活动商品");
		try{
			AddSellerProductRequest addpro =  ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();

			String price = "9.0";
			Date beginTime1 = YMTDateUtil.parseYMDHMSDate(YMTDateUtil.getBeforeOrNextSecond(-3600));
			Date endTime1 = YMTDateUtil.parseYMDHMSDate(YMTDateUtil.getBeforeOrNextSecond(3600));
			int aid = ProductPriceCallService.ApplyHighActivity(price,productId,beginTime1,endTime1).getActivityId();

			JSONObject ret = new CheckProductExistsActivityCall().call(sellerId, productId);
			
			checkProductExistsActivity(productId,Arrays.asList(aid),ret);
		}catch (Exception e) {
			Logger.fail(e);
		}
	}



	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CheckProductExistsActivity_003() throws Exception {
		Logger.start(true,"获取商品参加活动信息 已结束活动商品");
		try{
			AddSellerProductRequest addpro =  ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();

			String price = "9.0";
			Date beginTime1 = YMTDateUtil.parseYMDHMSDate(YMTDateUtil.getBeforeOrNextSecond(-7600));
			Date endTime1 = YMTDateUtil.parseYMDHMSDate(YMTDateUtil.getBeforeOrNextSecond(-3600));
			int aid = ProductPriceCallService.ApplyHighActivity(price,productId,beginTime1,endTime1).getActivityId();
			JSONObject ret = new CheckProductExistsActivityCall().call(sellerId, productId);
			//活动已结束

			checkProductExistsActivity(productId,Arrays.asList(aid),ret);
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CheckProductExistsActivity_004() throws Exception {
		Logger.start(true,"获取商品参加活动信息 未开始活动");
		try{
			AddSellerProductRequest addpro =  ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();

			String price = "9.0";
			Date beginTime1 = YMTDateUtil.parseYMDHMSDate(YMTDateUtil.getBeforeOrNextSecond(3600));
			Date endTime1 = YMTDateUtil.parseYMDHMSDate(YMTDateUtil.getBeforeOrNextSecond(7600));
			int aid = ProductPriceCallService.ApplyHighActivity(price,productId,beginTime1,endTime1).getActivityId();
			
			JSONObject ret = new CheckProductExistsActivityCall().call(sellerId, productId);
			
			checkProductExistsActivity(productId,Arrays.asList(aid),ret);
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CheckProductExistsActivity_005() throws Exception {
		Logger.start(true,"获取商品参加活动信息 同时两场活动");
		try{
			AddSellerProductRequest addpro =  ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);

			String price = "9.0";
			Date beginTime1 = YMTDateUtil.parseYMDHMSDate(YMTDateUtil.getBeforeOrNextSecond(-3600));
			Date endTime1 = YMTDateUtil.parseYMDHMSDate(YMTDateUtil.getBeforeOrNextSecond(3600));
			int aid1 = ProductPriceCallService.ApplyHighActivity(price,productId,beginTime1,endTime1).getActivityId();
			int aid2 = ProductPriceCallService.ApplyLowActivity(price,productId,beginTime1,endTime1).getActivityId();

			
			JSONObject ret = new CheckProductExistsActivityCall().call(sellerId, productId);
			
			checkProductExistsActivity(productId,Arrays.asList(aid1,aid2),ret);
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	
	public static void checkProductExistsActivity(String productId,List<Integer> aids,JSONObject ret) throws Exception{
		Logger.verifyEquals(200, ret.getInt("Code"), "返回码");
		
		CheckProductInActivityResp actBean = ProductManagerCallService.getBeanFromJson(ret.getJSONObject("Data"), CheckProductInActivityResp.class);
	
		CheckProductInActivityResp tgtBean = getCheckProductInActivityRespBean(productId,aids);
		
		AssertHelper.assertResultEqual(tgtBean, actBean, "checkProductExistsActivity");
	
	}
	
	private static CheckProductInActivityResp getCheckProductInActivityRespBean(String productId,List<Integer> aids) throws Exception{
		CheckProductInActivityResp retBean = new CheckProductInActivityResp();
		
		String jsonBeanStr = HttpClientUtil.requestGet("http://selleractivity.iapi.ymatou.com/api/ActivityProduct/CheckProductInActivity", 
				MapUtil.hashMap("SellerId",sellerId.toString(),"ProductId",productId));
		
		retBean.setProductActivityEditFlag(new JSONObject(jsonBeanStr).getJSONObject("Data").getInt("EditState"));
		
		if (aids.size()>0){
			retBean.setIsCanEdit(false);
		}else{
			retBean.setIsCanEdit(true);
			retBean.setProductInActivityInfo(null);
			return retBean;
		}
		
		List<Map<String,Object>> actProdMapList = intergratedProductMapper.selectActivityProdByProdId(productId);
		List<ProductInActivityInfoBean> ProductInActivityInfoBeanList = new ArrayList<>();
		for(int aid:aids){
			Map<String,Object> activityMap = intergratedProductMapper.selectActivityByActivityId(aid);
			Map<String,Object> activityProdMap = actProdMapList.stream().filter(x->Integer.parseInt(x.get("iActivityId").toString())==aid).findFirst().orElse(null);
		//	Logger.verifyNotNull(activityProdMap, "productid:"+productId+",aid:"+aid+"不用为null");
			
			ProductInActivityInfoBean productInActivityInfoBean = new ProductInActivityInfoBean();
			productInActivityInfoBean.setActivityId(aid);
			productInActivityInfoBean.setActivityName(activityMap.get("sName").toString());
			productInActivityInfoBean.setStatus((Integer)activityProdMap.get("iStatus"));
			
			ProductInActivityInfoBeanList.add(productInActivityInfoBean);
			
		}
		
		
		retBean.setProductInActivityInfo(ProductInActivityInfoBeanList);
		
		return retBean;
		
	}
	
	
	
}
